package flow.action;

import flow.model.FlowSection;
import flow.model.FlowSectionLink;
import flow.service.FlowSectionService;
import m.common.action.ActionMeta;
import m.common.action.VueResult;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.common.model.util.QueryOrder;
import m.html.VueFormResult;
import m.html.VueListResult;
import m.html.element.attr.Align;
import m.html.element.attr.ButtonType;
import m.html.element.other.Param;
import m.html.form.*;
import m.html.list.ListButton;
import m.html.list.ListDropdown;
import m.html.list.ListSortOrder;
import m.html.list.SuccessMethod;
import m.system.exception.MException;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import manage.action.ManageAction;

@ActionMeta(name="flow/flowSection")
public class FlowSectionAction extends ManageAction {
	private FlowSection model;
	private FlowSectionLink link;

	public JSONMessage doSave() throws Exception {
		systemLog("流程","保存流程环节信息");
		verifyAdminOperPower("manage_flow_power");
		String msg=getService(FlowSectionService.class).save(model);
		return new JSONMessage(0,msg);
	}
	public JSONMessage doDelete() throws Exception {
		systemLog("流程","删除流程环节信息");
		verifyAdminOperPower("manage_flow_power");
		getService(FlowSectionService.class).delete(model);
		return new JSONMessage(0,"删除成功");
	}
	public JSONMessage doVerify() throws Exception {
		systemLog("流程","验证流程环节信息");
		verifyAdminOperPower("manage_flow_power");
		getService(FlowSectionService.class).verifySection(model.getFlowDefine().getOid());
		return new JSONMessage(0,"验证通过");
	}
	public JSONMessage doSaveNext() throws Exception {
		systemLog("流程","保存流程链接信息");
		verifyAdminOperPower("manage_flow_power");
		String msg=getService(FlowSectionService.class).saveNext(link);
		return new JSONMessage(0,msg);
	}
	public JSONMessage doDeleteNext() throws Exception {
		systemLog("流程","删除流程链接信息");
		verifyAdminOperPower("manage_flow_power");
		getService(FlowSectionService.class).deleteNext(link);
		return new JSONMessage(0,"删除成功");
	}
	public VueResult toEdit() throws Exception {
		verifyAdminOperPower("manage_flow_power");
		if(null!=model&&!StringUtil.isSpace(model.getOid())){
			model=ModelQueryList.getModel(model,"*");
		}else if(null!=model&&null!=model.getFlowDefine()&&!StringUtil.isSpace(model.getFlowDefine().getOid())){
			model.setCountersign("N");
			model.setForwardable("N");
			model.setSaveable("N");
		}else {
			throw new MException(this.getClass(), "参数错误");
		}
		ModelForm form = new ModelForm("流程环节",2);
		form.addItem(
			FormInput.input("标识", "model.identity").setPlaceholder("请输入标识").setRequired(true).setSpan(2),
			FormInput.input("名称", "model.name").setPlaceholder("请输入名称").setRequired(true).setSpan(2),
			FormInput.input("组", "model.block").setPlaceholder("请输入组"),
			FormInput.number("排序", "model.sort","#,##0",0).setPlaceholder("请输入排序"),
			FormInput.radio("可暂存", "model.saveable", "flow.model.FlowSection!saveable").setPlaceholder("请选择可暂存").setSpan(2),
			FormInput.radio("可转发", "model.forwardable", "flow.model.FlowSection!forwardable").setPlaceholder("请选择可转发").setSpan(2),
			FormInput.radio("会签", "model.countersign", "flow.model.FlowSection!countersign").setPlaceholder("请选择会签").setSpan(2)
					.setDescription("会签环节的所有人都通过才能进入下一步，否则走退回环节。")
		)
		.addData("model",model)
		.addButton(
			FormButton.pri("保存","doSave")
		)
		.addMethod(
			FormEvent.backFlag,
			FormEvent.verify("verifyModel",
				FormJsTest.falseRemind("this.model.identity","标识不能为空"),
				FormJsTest.falseRemind("/^[\\da-z\\-_]+$/i.test(this.model.identity)","标识只能输入数字字母和横线"),
				FormJsTest.falseRemind("this.model.name","名称不能为空")
			),
			FormEvent.doJson("doSave",actionUrl("doSave"),"{model:this.model}")
				.before("verifyModel").success("backFlag")
		);
		VueFormResult result=new VueFormResult();
		return result.addForm(form);
	}
	public VueResult toEditNext() throws Exception {
		verifyAdminOperPower("manage_flow_power");
		if(null!=link&&!StringUtil.isSpace(link.getOid())){
			link=ModelQueryList.getModel(link,"*","fromSection.*");
		}else if(null!=link&&null!=link.getFromSection()&&!StringUtil.isSpace(link.getFromSection().getOid())){
			link.setFromSection(ModelQueryList.getModel(link.getFromSection(), "*"));
			link.setIsNext("N");
			link.setToOrgOption("N");
		}else {
			throw new MException(this.getClass(), "参数错误");
		}

		ModelForm form = new ModelForm("环节链接",2);
		form.addItem(
			FormHtml.ins("环节","{{model.fromSection.name}}({{model.fromSection.identity}})").setSpan(2),
			FormInput.radio("下一步", "model.isNext", "flow.model.FlowSectionLink!isNext").setPlaceholder("请选择下一步").setSpan(2),
			FormTest.test("model.isNext=='Y'||model.isNext=='B'",
				FormInput.selectOneModel("下一环节","model","toSection",
				"'"+actionUrl("selectSection")+"?params[flowDefineOid]='+model.fromSection.flowDefine.oid",600,
				"{{model.toSection.identity}} {{model.toSection.name}}").setSpan(2)),
			FormTest.test("model.isNext=='Y'||model.isNext=='N'",
				FormInput.input("参数", "model.toParam").setPlaceholder("请输入下一步参数")),
			FormInput.input("描述", "model.toDesc").setPlaceholder("请输入下一步描述").setSpanVal("model.isNext=='B'?2:1"),
			FormTest.test("model.isNext=='Y'",
				FormInput.radio("选项", "model.toOption", "flow.model.FlowSectionLink!toOption").setSpan(2)),
			FormTest.test("model.isNext=='Y'&&(model.toOption=='MU'||model.toOption=='OU')",
					FormInput.radio("部门限制", "model.toOrgOption", "flow.model.FlowSectionLink!toOrgOption").setSpan(2)),
			FormTest.test("model.isNext=='Y'&&model.toOrgOption=='Y'&&(model.toOption=='MU'||model.toOption=='OU')",
				FormInput.selectTree("可选部门", "model.toOrg", "manage.model.AdminGroup:C",true,true).setPlaceholder("请选择下一步可选部门").setSpan(2)),
			FormTest.test("model.isNext=='Y'&&(model.toOption=='MU'||model.toOption=='OU')",
				FormInput.selectItem("角色限制", "model.toRole", "manage.model.AdminGroup:B",true).setPlaceholder("请选择下一步角色限制").setSpan(2))
		)
		.addData("model",link)
		.addButton(
			FormButton.pri("保存","doSave")
		)
		.addMethod(
			FormEvent.backTrue,
			FormEvent.doJson("doSave",actionUrl("doSaveNext"),"{link:this.model}")
				.success("backTrue")
		);
		VueFormResult result=new VueFormResult();
		return result.addForm(form);
	}
	public VueResult selectSection(){
		VueListResult result=getVueListResult("选择流程环节",FlowSection.class,actionUrl("selectSection"));
		result.setMergeCellsInfo(1,1);
		result.addQueryFields("oid");
		result.addCol(toIndexColumn());
		result.addCol(toTextColumn("组", "block", 100, true));
		result.addCol(toTextColumn("标识", "identity", 100, true));
		result.addCol(toTextColumn("名称", "name", 100, true));
		result.addCol(toIntegerColumn("排序", "sort", 100, true).setSort(ListSortOrder.ASC));
		result.addCol(toSpaceColumn("操作", 100,
			ListButton.back("选择", ButtonType.PRIMARY).addParam("","")
		).setFixed(Align.RIGHT));
		result.addCondition(QueryCondition.eq("flowDefine.oid",getParamsValue("flowDefineOid")));
		return result;
	}
	public VueResult flowSectionList() throws Exception{
		String flowDefineOid=getParamsValue("flowDefineOid");
		boolean isEdit= getParamsValue("oper").equals("edit")&&StringUtil.notSpace(flowDefineOid);
		boolean power=hasAdminOperPower("manage_flow_power");
		VueListResult result=getVueListResult("流程环节",FlowSectionLink.class,actionUrl("flowSectionList"));
		result.setSubTitle("添加或者编辑流程信息。");
		result.setMergeCellsInfo(0,8);
		result.addOrder(QueryOrder.asc("fromSection.sort"), QueryOrder.desc("isNext"));
		//result.addCol(toIndexColumn().setFixed(Align.LEFT));
		result.addCol(toTextColumn("组", "fromSection.block",50).setFixed(Align.LEFT));
		result.addCol(toTextColumn("排序", "fromSection.sort",50).setFixed(Align.LEFT));
		result.addCol(toTextColumn("标识", "fromSection.identity",50).setFixed(Align.LEFT));
		result.addCol(toTextColumn("名称", "fromSection.name",100).setFixed(Align.LEFT));
		result.addCol(toTextColumn("转发", "fromSection.forwardable",50).setDictOption(FlowSection.class,"forwardable"));
		result.addCol(toTextColumn("暂存", "fromSection.saveable",50).setDictOption(FlowSection.class,"saveable"));
		result.addCol(toTextColumn("会签", "fromSection.countersign",50).setDictOption(FlowSection.class,"countersign"));
		if(power&&isEdit) result.addCol(toSpaceColumn("操作",
			ListDropdown.primary("操作").add(
				ListButton.modal("修改",ButtonType.PRIMARY,actionUrl("toEdit"),600,SuccessMethod.REFRESH)
					.addParam("model.oid","fromSection.oid"),
				ListButton.ajax("删除", ButtonType.ERROR, actionUrl("doDelete"), SuccessMethod.REFRESH)
					.addParam("model.oid", "fromSection.oid")
					.setConfirm("确定要删除该环节和所有环节链接吗?"),
				ListButton.modal("添加环节链接",ButtonType.PRIMARY,actionUrl("toEditNext"),700,SuccessMethod.REFRESH)
					.addParam("link.fromSection.oid","fromSection.oid")
			)
		));
		result.addCol(toTextColumn("下一步", "isNext",60).setDictOption(FlowSectionLink.class,"isNext"));
		result.addCol(toTextColumn("环节", "toSection.name",100));
		result.addCol(toTextColumn("参数", "toParam",60));
		result.addCol(toTextColumn("描述", "toDesc"));
		result.addCol(toTextColumn("选项", "toOption",80).setDictOption(FlowSectionLink.class,"toOption"));
		result.addCol(toTextColumn("部门限制", "toOrgOption",80).setDictOption(FlowSectionLink.class,"toOrgOption"));
		result.addCol(toTextColumn("可选部门", "toOrg",100).setDictType("manage.model.AdminGroup:C"));
		result.addCol(toTextColumn("角色限制", "toRole",100).setDictType("manage.model.AdminGroup:B"));

		if(power&&isEdit) result.addCol(toSpaceColumn("操作",
			ListDropdown.primary("操作").add(
				ListButton.modal("修改",ButtonType.PRIMARY,actionUrl("toEditNext"),700,SuccessMethod.REFRESH)
					.addParam("link.oid","oid"),
				ListButton.ajax("删除", ButtonType.ERROR, actionUrl("doDeleteNext"), SuccessMethod.REFRESH)
					.addParam("link.oid", "oid")
					.setConfirm("确定要删除该环节链接吗?")
			)
		).setFixed(Align.RIGHT));
		if(power&&isEdit) result.addButton(
			ListButton.modal("新增环节",ButtonType.PRIMARY,actionUrl("toEdit"),600,SuccessMethod.REFRESH)
				.addQueryParam(Param.value("model.flowDefine.oid", flowDefineOid)),
			ListButton.ajax("验证环节",ButtonType.SUCCESS,actionUrl("doVerify"))
				.addQueryParam(Param.value("model.flowDefine.oid", flowDefineOid))
		);
		result.addButton(
			ListButton.modal("流程图",ButtonType.DEFAULT,"page/base/flow/viewFlowDefine.js",800)
				.addQueryParam(Param.value("defineOid", flowDefineOid))
		);
		result.addCondition(QueryCondition.eq("fromSection.flowDefine.oid",flowDefineOid));
		return result;
	}

	public FlowSection getModel() {
		return model;
	}

	public void setModel(FlowSection model) {
		this.model = model;
	}

	public FlowSectionLink getLink() {
		return link;
	}

	public void setLink(FlowSectionLink link) {
		this.link = link;
	}
	
	
}
